package ldh.im.fxgui.pane;

import javafx.geometry.HPos;
import javafx.geometry.Pos;
import javafx.geometry.VPos;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.ImageView;
import javafx.scene.layout.*;
import ldh.im.dto.enums.MessageType;
import ldh.im.fxbase.data.pojo.ImMessage;
import ldh.im.fxbase.data.pojo.ImSession;
import ldh.im.fxbase.data.pojo.ImUser;
import ldh.im.fxbase.util.ImCacheFactory;
import ldh.im.fxbase.util.MessageUtil;
import ldh.im.fxgui.component.ImStage;
import ldh.im.fxgui.util.UserHeadImageUtil;
import ldh.im.util.DateUtil;

import java.util.List;

/**
 * 多个消息合并
 */
public class ChatMessagePane extends BaseMessagePane {

    private ImStage imStage = null;

    @Override
    public Node createPane(ImMessage imMessage, boolean isReceive, boolean isRef) {
        if (imMessage.getMessageType() != MessageType.Chat) {
            throw new RuntimeException("不支持这种消息类型" + imMessage.getMessageType());
        }

        VBox vBox = new VBox();
        vBox.getStyleClass().add("chat-item");
        vBox.setAlignment(Pos.TOP_LEFT);
        List<ImMessage> refMessages = imMessage.getRefMessages();
        if (refMessages == null) {
            throw new RuntimeException("请对引用信息进行加载");
        }

        ImSession session = refMessages.get(0).getImSession();

        String sessionName = "群聊";
        if (session != null) {
            sessionName = session.getName();
        }
        Label text = new Label(sessionName + "的聊天记录");
        text.getStyleClass().add("chat-list-head");
        vBox.getChildren().add(text);

        for (ImMessage message : refMessages) {
            MessageType messageType = message.getMessageType();
            String content = MessageUtil.buildLastedMessage(messageType, message.getContent());
            ImUser fromUser = ImCacheFactory.getInstance().getUser(message.getFromUserId());
            Label refNode = new Label(fromUser.getUserName() + "：" +content);
            refNode.getStyleClass().add("chat-list-item");
            vBox.getChildren().add(refNode);
        }

        HBox hBox = new HBox();
        hBox.getStyleClass().add("chat-list-bottom");
        Label label = new Label("聊天记录");
        hBox.getChildren().add(label);
        vBox.getChildren().add(hBox);

        vBox.setOnMouseClicked(e->{
            if (e.getClickCount() == 2) {
                showDialogPane(imMessage);
            }
        });
        return vBox;
    }

    private void showDialogPane(ImMessage imMessage) {
        if (imStage == null) {
            imStage = new ImStage(600, 500);
            imStage.getCurrentScene().getStylesheets().add(this.getClass().getResource("/css/message-dialog.css").toExternalForm());
            ImSession imSession = imMessage.getRefMessages().get(0).getImSession();
            imStage.setTitle(imSession.getName());

            ScrollPane scrollPane = new ScrollPane();
            scrollPane.getStyleClass().addAll("noborder-scroll-pane", "chat-dialog-pane");
            VBox vBox = new VBox();
            vBox.setAlignment(Pos.TOP_CENTER);
            for (ImMessage refMessage : imMessage.getRefMessages()) {
                vBox.getChildren().add(buildMessagePane(refMessage));
            }
            scrollPane.setContent(vBox);
            imStage.setContent(scrollPane);
        }
        imStage.show();
    }

    private GridPane buildMessagePane(ImMessage refMessage) {
        GridPane gridPane = new GridPane();
        gridPane.getStyleClass().add("chat-item");

        String fromUserId = refMessage.getFromUserId();
        ImUser fromUser = ImCacheFactory.getInstance().getUser(fromUserId);
        ImageView imageView = new ImageView(UserHeadImageUtil.getHeadImage(fromUser.getHeadImg()));
        imageView.setFitHeight(50);
        imageView.setFitWidth(50);
        Label label = new Label();
        label.setGraphic(imageView);
        label.setMaxSize(50, 50);
        gridPane.getChildren().add(label);
        GridPane.setConstraints(label, 0, 0, 2, 2, HPos.CENTER, VPos.TOP);

        Label fromUserNameLabel = new Label(fromUser.getUserName());
        gridPane.getChildren().add(fromUserNameLabel);
        GridPane.setConstraints(fromUserNameLabel, 2, 0);

        Label timeLabel = new Label(DateUtil.format(refMessage.getServerReceiveTime(), "yyyy-MM-dd HH:mm:ss"));
        gridPane.getChildren().add(timeLabel);
        GridPane.setConstraints(timeLabel, 3, 0);

        MessageType messageType = refMessage.getMessageType();
        MessagePane messagePane = MessagePaneEnum.getByMessageType(messageType).getMessagePane();
        Node node = messagePane.createViewPane(null, refMessage, true);
        gridPane.getChildren().add(node);
        GridPane.setConstraints(node, 2, 1, 2, 2);

        ColumnConstraints c1 = new ColumnConstraints();
        ColumnConstraints c2 = new ColumnConstraints();
        ColumnConstraints c3 = new ColumnConstraints();
        c3.setHalignment(HPos.LEFT);
        c3.setHgrow(Priority.ALWAYS);
        ColumnConstraints c4 = new ColumnConstraints();
        gridPane.getColumnConstraints().addAll(c1, c2, c3, c4);

        gridPane.setGridLinesVisible(false);
        return gridPane;
    }
}
